package com.fowo.api.syncLingxingApi.service.syncImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.syncLingxingApi.entity.TmpSyncLingxingToWarehousebin;
import com.fowo.api.syncLingxingApi.service.SyncLxApiBaseService;
import com.fowo.api.syncLingxingApi.service.TmpSyncLingxingToWarehousebinService;
import com.fowo.api.syncLingxingApi.service.impl.TmpSyncLingxingToWarehousebinServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@DS("ds24")
public class SyncLxWarehousebinServiceImpl
        extends SyncLxApiBaseService<TmpSyncLingxingToWarehousebin, TmpSyncLingxingToWarehousebin>  {
    private final static String LX_API_URL_KEY = "warehouseBinUrl";
    private final static int MAX_LEN = 1000;

    @Resource
    private TmpSyncLingxingToWarehousebinServiceImpl tmpService;

    public SyncLxWarehousebinServiceImpl() {
        super(LX_API_URL_KEY, MAX_LEN);
    }

    @Override
    protected void beforeLoop() {
        super.beforeLoop();
        tmpService.clearAll();
//        this.curOffset = 8920;
//        this.totalNum = 9000;
    }

    @Override
    protected JSONObject pullLingxingData(long offset, long length, JSONObject params) {
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("offset", offset);
        reqBody.put("length", length);
        reqBody.put("limit", length);
        if (params != null) {
            if (params.containsKey("wid")) reqBody.put("wid", params.getString("wid"));
            if (params.containsKey("id")) reqBody.put("id", params.getString("id"));
            if (params.containsKey("status")) reqBody.put("status", params.getString("status"));
            if (params.containsKey("type")) reqBody.put("type", params.getString("type"));
        }
        return this.tryRequestLingxingApi(reqBody);
    }

//    @Override
//    protected boolean loopCond() {
//        return false;
//    }

    @Override
    protected boolean cmpNewAndOldEqualCond(TmpSyncLingxingToWarehousebin newOne, TmpSyncLingxingToWarehousebin oldOne) {
        if (newOne == null || oldOne == null)
            return false;
        return true;
//        String newWid = newOne.getWid() == null? "": newOne.getWid();
//        String oldWid = oldOne.getWid() == null? "": oldOne.getWid();
//        String newBin = newOne.getStorageBin() == null? "": newOne.getStorageBin();
//        String oldBin = oldOne.getStorageBin() == null? "": oldOne.getStorageBin();
//        String newSku = newOne.getSku() == null? "": newOne.getSku();
//        String oldSku = oldOne.getSku() == null? "": oldOne.getSku();
//        String newFnsku = newOne.getFnsku() == null? "": newOne.getFnsku();
//        String oldFnsku = oldOne.getFnsku() == null? "": oldOne.getFnsku();
//        long newStoreId = newOne.getStoreId();
//        long oldStoreId = oldOne.getStoreId();
//
//        return newWid.equals(oldWid)
//                && newBin.equals(oldBin)
//                && newSku.equals(oldSku)
//                && newFnsku.equals(oldFnsku)
//                && newStoreId == oldStoreId;

    }

    @Override
    protected TmpSyncLingxingToWarehousebin cmpNewAndOldInsertProc(TmpSyncLingxingToWarehousebin newOne) {
        newOne.set_flag(2);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected TmpSyncLingxingToWarehousebin cmpNewAndOldUpdateProc(TmpSyncLingxingToWarehousebin newOne, TmpSyncLingxingToWarehousebin oldOne) {
        newOne.set_flag(1);
        newOne.set_version(this.data_version);
        return newOne;
    }

    @Override
    protected boolean cmpNewAndOldUpdateCond(TmpSyncLingxingToWarehousebin newOne, TmpSyncLingxingToWarehousebin oldOne) {
        return newOne != null && oldOne != null;
    }

    public void saveAll(String version) {
        this.data_version = version;
        this.saveTempToFormal();
    }

    @Override
    protected void saveTempToFormal() {
        log.info("save to warehouse_bin;");
        tmpService.saveWarehouseBin(this.data_version);
        log.info("save to warehouse_bin_sku;");
        tmpService.saveWarehouseBinSkus(this.data_version);
    }

    @Override
    protected List<TmpSyncLingxingToWarehousebin> jsonDumpsResult(JSONArray jsonArray) {
        List<TmpSyncLingxingToWarehousebin> tmpSyncLingxingToWarehousebinList = new ArrayList<>();

        for (Object obj: jsonArray) {
            JSONObject jsonObject = (JSONObject) obj;
            String wid = jsonObject.getString("wid");
            String wareHouseName = jsonObject.getString("ware_house_name");
            int storageBinId = jsonObject.getInteger("id");

//            if (storageBinId == 14830) {
//                log.info("here.");
//                log.info(jsonObject.toJSONString());
//                System.exit(0);
//            }
//            else {
//                continue;
//            }

            String storageBin = jsonObject.getString("storage_bin");
            String whbStatus = jsonObject.getString("whb_status");
            String type = jsonObject.getString("type");

            JSONArray sku_fnsku = jsonObject.getJSONArray("sku_fnsku");

            if (sku_fnsku == null || sku_fnsku.isEmpty()) {
                TmpSyncLingxingToWarehousebin newObj = new TmpSyncLingxingToWarehousebin();
                newObj.setWid(wid);
                newObj.setWareHouseName(wareHouseName);
                newObj.setStorageBinId(storageBinId);
                newObj.setStorageBin(storageBin);
                newObj.setWhbStatus(whbStatus);
                newObj.setType(type);
                tmpSyncLingxingToWarehousebinList.add(newObj);
            }
            else {
                for (Object item : sku_fnsku) {
                    TmpSyncLingxingToWarehousebin newObj = new TmpSyncLingxingToWarehousebin();
                    newObj.setWid(wid);
                    newObj.setWareHouseName(wareHouseName);
                    newObj.setStorageBinId(storageBinId);
                    newObj.setStorageBin(storageBin);
                    newObj.setWhbStatus(whbStatus);
                    newObj.setType(type);

                    JSONObject jObj = (JSONObject) item;
                    newObj.setFnsku(jObj.getString("fnsku"));
                    newObj.setSku(jObj.getString("sku"));
                    newObj.setProductId(jObj.getInteger("product_id"));
                    newObj.setStoreId(jObj.getLong("store_id"));
                    newObj.setSellerName(jObj.getString("seller_name"));
                    newObj.setProductName(jObj.getString("product_name"));

                    tmpSyncLingxingToWarehousebinList.add(newObj);
                }
            }

        }

        return tmpSyncLingxingToWarehousebinList;
    }

    @Override
    protected List<TmpSyncLingxingToWarehousebin> getOldData(List<TmpSyncLingxingToWarehousebin> newObjects) {
        if (newObjects == null || newObjects.isEmpty())
            return null;
        return tmpService.getWarehouseBin(newObjects);
    }

    @Override
    protected void saveData2TmpTb(List<TmpSyncLingxingToWarehousebin> results) {
        tmpService.saveBatch(results);
    }

    @Resource
    TmpSyncLingxingToWarehousebinService tmpSyncLingxingToWarehousebinService;

    public void runById() {
        List<String> ids = tmpSyncLingxingToWarehousebinService.getWarehouseId();
        for (String id : ids) {
            JSONObject requestBody = new JSONObject();
            requestBody.put("id", id);
            this.mainLoop(requestBody);
        }
    }

}
